﻿
namespace Indeco.G24.Windows.Forms
{
	/// <summary>
	/// abstract NguoiDungVaiTro typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class NguoiDungVaiTroDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<NguoiDungVaiTroDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.NguoiDungVaiTro _currentNguoiDungVaiTro = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxNguoiDungVaiTroDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxNguoiDungVaiTroErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxNguoiDungVaiTroBindingSource;
		
		/// <summary> 
		/// the DGV column associated with the IDVaiTro property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDVaiTroDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IDNguoiDung property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDNguoiDungDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.NguoiDung> _IDNguoiDungList;
		
		/// <summary> 
		/// The list of selectable NguoiDung
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.NguoiDung> IDNguoiDungList
		{
			get {return this._IDNguoiDungList;}
			set 
			{
				this._IDNguoiDungList = value;
				this.uxIDNguoiDungDataGridViewColumn.DataSource = null;
				this.uxIDNguoiDungDataGridViewColumn.DataSource = this._IDNguoiDungList;
			}
		}
		
		private bool _allowNewItemInIDNguoiDungList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of NguoiDung
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDNguoiDungList
		{
			get { return _allowNewItemInIDNguoiDungList;}
			set
			{
				this._allowNewItemInIDNguoiDungList = value;
				this.uxIDNguoiDungDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
				
		private Entities.TList<Entities.VaiTro> _IDVaiTroList;
		
		/// <summary> 
		/// The list of selectable VaiTro
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.VaiTro> IDVaiTroList
		{
			get {return this._IDVaiTroList;}
			set 
			{
				this._IDVaiTroList = value;
				this.uxIDVaiTroDataGridViewColumn.DataSource = null;
				this.uxIDVaiTroDataGridViewColumn.DataSource = this._IDVaiTroList;
			}
		}
		
		private bool _allowNewItemInIDVaiTroList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of VaiTro
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDVaiTroList
		{
			get { return _allowNewItemInIDVaiTroList;}
			set
			{
				this._allowNewItemInIDVaiTroList = value;
				this.uxIDVaiTroDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.NguoiDungVaiTro> _NguoiDungVaiTroList;
				
		/// <summary> 
		/// The list of NguoiDungVaiTro to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.NguoiDungVaiTro> NguoiDungVaiTroList
		{
			get {return this._NguoiDungVaiTroList;}
			set
			{
				this._NguoiDungVaiTroList = value;
				this.uxNguoiDungVaiTroBindingSource.DataSource = null;
				this.uxNguoiDungVaiTroBindingSource.DataSource = value;
				this.uxNguoiDungVaiTroDataGridView.DataSource = null;
				this.uxNguoiDungVaiTroDataGridView.DataSource = this.uxNguoiDungVaiTroBindingSource;				
				//this.uxNguoiDungVaiTroBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxNguoiDungVaiTroBindingSource_ListChanged);
				this.uxNguoiDungVaiTroBindingSource.CurrentItemChanged += new System.EventHandler(OnNguoiDungVaiTroBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnNguoiDungVaiTroBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentNguoiDungVaiTro = uxNguoiDungVaiTroBindingSource.Current as Entities.NguoiDungVaiTro;
			
			if (_currentNguoiDungVaiTro != null)
			{
				_currentNguoiDungVaiTro.Validate();
			}
			//_NguoiDungVaiTro.Validate();
			OnCurrentEntityChanged();
		}

		//void uxNguoiDungVaiTroBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.NguoiDungVaiTro"/> instance.
		/// </summary>
		public Entities.NguoiDungVaiTro SelectedNguoiDungVaiTro
		{
			get {return this._currentNguoiDungVaiTro;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxNguoiDungVaiTroDataGridView.VirtualMode;}
			set
			{
				this.uxNguoiDungVaiTroDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxNguoiDungVaiTroDataGridView.AllowUserToAddRows;}
			set {this.uxNguoiDungVaiTroDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxNguoiDungVaiTroDataGridView.AllowUserToDeleteRows;}
			set {this.uxNguoiDungVaiTroDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxNguoiDungVaiTroDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxNguoiDungVaiTroDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="NguoiDungVaiTroDataGridViewBase"/> class.
		/// </summary>
		public NguoiDungVaiTroDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxNguoiDungVaiTroDataGridView = new System.Windows.Forms.DataGridView();
			this.uxNguoiDungVaiTroBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxNguoiDungVaiTroErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIDVaiTroDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxIDNguoiDungDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			//this.uxIDNguoiDungBindingSource = new NguoiDungBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDNguoiDungBindingSource)).BeginInit();
			//this.uxIDVaiTroBindingSource = new VaiTroBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDVaiTroBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNguoiDungVaiTroDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNguoiDungVaiTroBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNguoiDungVaiTroErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxNguoiDungVaiTroErrorProvider
			// 
			this.uxNguoiDungVaiTroErrorProvider.ContainerControl = this;
			this.uxNguoiDungVaiTroErrorProvider.DataSource = this.uxNguoiDungVaiTroBindingSource;						
			// 
			// uxNguoiDungVaiTroDataGridView
			// 
			this.uxNguoiDungVaiTroDataGridView.AutoGenerateColumns = false;
			this.uxNguoiDungVaiTroDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxNguoiDungVaiTroDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIDVaiTroDataGridViewColumn,
		this.uxIDNguoiDungDataGridViewColumn			});
			this.uxNguoiDungVaiTroDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxNguoiDungVaiTroDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxNguoiDungVaiTroDataGridView.Name = "uxNguoiDungVaiTroDataGridView";
			this.uxNguoiDungVaiTroDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxNguoiDungVaiTroDataGridView.TabIndex = 0;	
			this.uxNguoiDungVaiTroDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxNguoiDungVaiTroDataGridView.EnableHeadersVisualStyles = false;
			this.uxNguoiDungVaiTroDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnNguoiDungVaiTroDataGridViewDataError);
			this.uxNguoiDungVaiTroDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnNguoiDungVaiTroDataGridViewCellValueNeeded);
			this.uxNguoiDungVaiTroDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnNguoiDungVaiTroDataGridViewCellValuePushed);
			
			//
			// uxIDVaiTroDataGridViewColumn
			//
			this.uxIDVaiTroDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDVaiTroDataGridViewColumn.DataPropertyName = "IDVaiTro";
			this.uxIDVaiTroDataGridViewColumn.HeaderText = "IDVaiTro";
			this.uxIDVaiTroDataGridViewColumn.Name = "uxIDVaiTroDataGridViewColumn";
			this.uxIDVaiTroDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDVaiTroDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDVaiTroDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDNguoiDungDataGridViewColumn
			//
			this.uxIDNguoiDungDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDNguoiDungDataGridViewColumn.DataPropertyName = "IDNguoiDung";
			this.uxIDNguoiDungDataGridViewColumn.HeaderText = "IDNguoiDung";
			this.uxIDNguoiDungDataGridViewColumn.Name = "uxIDNguoiDungDataGridViewColumn";
			this.uxIDNguoiDungDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDNguoiDungDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDNguoiDungDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDNguoiDungDataGridViewColumn
			//				
			this.uxIDNguoiDungDataGridViewColumn.DisplayMember = "TenSuDung";	
			this.uxIDNguoiDungDataGridViewColumn.ValueMember = "IDNguoiDung";	
			this.uxIDNguoiDungDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDNguoiDungDataGridViewColumn.DataSource = uxIDNguoiDungBindingSource;				
				
			//
			// uxIDVaiTroDataGridViewColumn
			//				
			this.uxIDVaiTroDataGridViewColumn.DisplayMember = "TenVaiTro";	
			this.uxIDVaiTroDataGridViewColumn.ValueMember = "IDVaiTro";	
			this.uxIDVaiTroDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDVaiTroDataGridViewColumn.DataSource = uxIDVaiTroBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxNguoiDungVaiTroDataGridView);
			this.Name = "NguoiDungVaiTroDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDNguoiDungBindingSource)).EndInit();
			//((System.ComponentModel.ISupportInitialize)(this.uxIDVaiTroBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNguoiDungVaiTroErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNguoiDungVaiTroDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNguoiDungVaiTroBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				NguoiDungVaiTroDataGridViewEventArgs args = new NguoiDungVaiTroDataGridViewEventArgs();
				args.NguoiDungVaiTro = _currentNguoiDungVaiTro;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class NguoiDungVaiTroDataGridViewEventArgs : System.EventArgs
		{
			private Entities.NguoiDungVaiTro	_NguoiDungVaiTro;
	
			/// <summary>
			/// the  Entities.NguoiDungVaiTro instance.
			/// </summary>
			public Entities.NguoiDungVaiTro NguoiDungVaiTro
			{
				get { return _NguoiDungVaiTro; }
				set { _NguoiDungVaiTro = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxNguoiDungVaiTroDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnNguoiDungVaiTroDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxNguoiDungVaiTroDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnNguoiDungVaiTroDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxNguoiDungVaiTroDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDVaiTroDataGridViewColumn":
						e.Value = NguoiDungVaiTroList[e.RowIndex].IDVaiTro;
						break;
					case "uxIDNguoiDungDataGridViewColumn":
						e.Value = NguoiDungVaiTroList[e.RowIndex].IDNguoiDung;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxNguoiDungVaiTroDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnNguoiDungVaiTroDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxNguoiDungVaiTroDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDVaiTroDataGridViewColumn":
						NguoiDungVaiTroList[e.RowIndex].IDVaiTro = (System.Guid)e.Value;
						break;
					case "uxIDNguoiDungDataGridViewColumn":
						NguoiDungVaiTroList[e.RowIndex].IDNguoiDung = (System.Guid)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
